<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Models\Fang;
use App\Http\Models\Notice;
use Illuminate\Http\Request;

//导入es生成索引类
use Elasticsearch\ClientBuilder;

class FangController extends Controller
{
    public function  index(){
        $data = Fang::with(['owner'])->paginate(20);
        return view('admin.fang.index',compact('data'));
    }

    //
    public function create(Fang $fang){
        //
//        $data = (new Fang())->relationData();

        $data = $fang->relationData();
        return view('admin.fang.add',$data);

    }

    public function store(){
        //添加到es
//        $client = ClientBuilder::create()->setHosts(config('es.host'))->build();
//        $params = [
//            'index' => 'fang',
//
//        ];

        Notice::create();

    }

    public function esinit(){
        //得到es客户端对象
        $client = ClientBuilder::create()->setHosts(config('es.host'))->build();
        //创建索引
        $params = [
            //生成索引名称
            'index' => 'fang',
            //类型body
//            'body' => [
//                'settings' => [
//                    //分区数
//                    'number_of_shards' => 5,
//                    //副本数
//                    'number_of_replicas' =>1
//                ],
//                'mappings' => [
//                    '_doc'=>[
//                        '_source' =>[
//                            'enabled' => true
//                        ],
//                        //字段 类似表字段,设置类型
//                        'properties' =>[
//                            'fang_name' =>[
//                                //相当于数据查询
//                                'type' => 'keyword'
//                            ],
//                            'fang_desn' => [
//                                'type' => 'text',
//                                //中文分词
//                                'analyzer' => 'ik_max_word',
//                                'search_analyzer' => 'ik_max_word'
//                            ]
//                        ]
//                    ]
//                ]
//            ]
        ];


        $params1 = [
            'id' => 'BigfiUfkTFWy43CiFYH1Mg',
            'index' => 'fang',
        ];



        //创建索引
//        $response = $client->indices()->create($params);
        $response = $client->delete($params1);
    }
}
